﻿// Dynamic tree editor - tree-like data structures editor for .NET/Mono.
// Copyright (C) 2006  Vladimir Sibirov
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace TreeEditor
{
	/// <summary>
	/// This dialog appers on column adding.
	/// </summary>
	public partial class NewColumnDialog
	{
		private Class cls;
		private string culture;
		
		/// <summary>
		/// Form constructor.
		/// </summary>
		public NewColumnDialog()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		/// <summary>
		/// Culture info for form communication.
		/// </summary>
		internal string Culture
		{
			set
			{
				culture = value;
			}
		}
		
		/// <summary>
		/// Parent class for form communication.
		/// </summary>
		internal Class ParentClass
		{
			set
			{
				cls = value;
			}
		}
		
		/// <summary>
		/// Column name validation.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void ColNameBoxValidating(object sender, System.ComponentModel.CancelEventArgs e)
		{
			// Checking the syntax
			Regex re = new Regex(@"^[\w_\ \-]+$");
			if(re.IsMatch(this.colNameBox.Text))
			{
				bool exists = false;
				for(int i = 0; i < cls.Columns.Count; i++)
				{
					if(((Column) cls.Columns.GetByIndex(i)).Name == this.colNameBox.Text)
					{
						exists = true;
						break;
					}
				}
				if(exists)
				{
					if(culture == "ru")
						MessageBox.Show("Имя поля должно быть уникальным.", "Неверный ввод");
					else
						MessageBox.Show("Column name must be unique.", "Wrong input");
					e.Cancel = true;
				}
				else
					e.Cancel = false;
			}
			else
			{
				if(culture == "ru")
					MessageBox.Show("Имя может содержать буквы, цифры, пробел, знаки подчеркивания и дефис.", "Неверный ввод");
				else
					MessageBox.Show("The name can contain alphanumeric characters, spaces, underscores and dashes.", "Wrong input");
				e.Cancel = true;
			}
		}
		
		/// <summary>
		/// Column caption validation.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void ColCaptionBoxValidating(object sender, System.ComponentModel.CancelEventArgs e)
		{
			/*
			// Empty caption is allowed
			if(this.colCaptionBox.Text == null)
			{
				e.Cancel = false;
				return;
			}
			else if(this.colCaptionBox.Text == "")
			{
				this.colCaptionBox.Text = this.colNameBox.Text;
				e.Cancel = false;
				return;
			}
			// Checking the syntax
			Regex re = new Regex(@"^[\w_\ \-]+$");
			if(re.IsMatch(this.colCaptionBox.Text))
			{
				e.Cancel = false;
			}
			else
			{
				if(culture == "ru")
					MessageBox.Show("Имя может содержать буквы, цифры, пробел, знаки подчеркивания и дефис.", "Неверный ввод");
				else
					MessageBox.Show("The name can contain alphanumeric characters, spaces, underscores and dashes.", "Wrong input");
				e.Cancel = true;
			}*/
			e.Cancel = false;
		}
		
		/// <summary>
		/// Column length validation.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void ColLengthBoxValidating(object sender, System.ComponentModel.CancelEventArgs e)
		{
			// Checking the syntax
			Regex re = new Regex(@"^\d{0,10}$");
			if(re.IsMatch(this.colLengthBox.Text))
			{
				long val = long.Parse(this.colLengthBox.Text);
				if(val > int.MaxValue)
				{
					if(culture == "ru")
						MessageBox.Show("Введено слишком большое число.", "Неверный ввод");
					else
						MessageBox.Show("The number is too big.", "Wrong input");
					e.Cancel = true;
				}
				else
					e.Cancel = false;
			}
			else
			{
				if(culture == "ru")
					MessageBox.Show("Введите число не длиннее 10 цифр.", "Неверный ввод");
				else
					MessageBox.Show("Enter a number not longer than 10 digits.", "Wrong input");
				e.Cancel = true;
			}
		}
		
		/// <summary>
		/// Turns auto-increment on.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void ColAutoBoxCheckedChanged(object sender, System.EventArgs e)
		{
			if(this.colAutoBox.Checked)
			{
				this.colDefaultBox.Enabled = false;
				this.colAllowNullBox.Checked = true;
				this.colAllowNullBox.Enabled = false;
				this.colAutoStartBox.Enabled = true;
				this.colAutoStepBox.Enabled = true;
				this.colReadOnlyBox.Enabled = true;
				this.colReadOnlyBox.Checked = true;
				this.colUniqueBox.Checked = true;
			}
			else
			{
				this.colDefaultBox.Enabled = true;
				this.colAllowNullBox.Enabled = true;
				this.colAutoStartBox.Enabled = false;
				this.colAutoStepBox.Enabled = false;
				this.colReadOnlyBox.Checked = false;
				this.colReadOnlyBox.Enabled = false;
				this.colUniqueBox.Checked = false;
			}
		}
		
		/// <summary>
		/// Auto increment seed validation.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void ColAutoStartBoxValidating(object sender, System.ComponentModel.CancelEventArgs e)
		{
			// Checking the syntax
			Regex re = new Regex(@"^\d{0,10}$");
			if(re.IsMatch(this.colAutoStartBox.Text))
			{
				e.Cancel = false;
			}
			else
			{
				if(culture == "ru")
					MessageBox.Show("Введите число не длиннее 10 цифр.", "Неверный ввод");
				else
					MessageBox.Show("Enter a number not longer than 10 digits.", "Wrong input");
				e.Cancel = true;
			}
		}
		
		/// <summary>
		/// Auto increment step validation.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void ColAutoStepBoxValidating(object sender, System.ComponentModel.CancelEventArgs e)
		{
			// Checking the syntax
			Regex re = new Regex(@"^\d{0,10}$");
			if(re.IsMatch(this.colAutoStepBox.Text))
			{
				e.Cancel = false;
			}
			else
			{
				if(culture == "ru")
					MessageBox.Show("Введите число не длиннее 10 цифр.", "Неверный ввод");
				else
					MessageBox.Show("Enter a number not longer than 10 digits.", "Wrong input");
				e.Cancel = true;
			}
		}
		
		/// <summary>
		/// Data type selection.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void ColTypeComboSelectedIndexChanged(object sender, System.EventArgs e)
		{
			// MaxLength is valid for string only
			if(this.colTypeCombo.SelectedIndex == 0)
				this.colLengthBox.Enabled = true;
			else
				this.colLengthBox.Enabled = false;
			// AutoIncrement is valid for integers only
			if(this.colTypeCombo.SelectedIndex > 0 && this.colTypeCombo.SelectedIndex < 4)
			{
				this.colAutoBox.Enabled = true;
			}
			else
			{
				this.colAutoBox.Checked = false;
				this.colAutoBox.Enabled = false;
			}
		}
		
		/// <summary>
		/// Proceeds with adding.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void BtnOKClick(object sender, System.EventArgs e)
		{
			// Check if the type is chosen
			if(this.colTypeCombo.SelectedIndex < 0)
			{
				if(culture == "ru")
					MessageBox.Show("Вы должны выбрать тип данных.", "Тип данных");
				else
					MessageBox.Show("You must select data type.", "Data type");
			}
			else if(!this.colAllowNullBox.Checked && (this.colDefaultBox.Text == null || this.colDefaultBox.Text == ""))
			{
				if(culture == "ru")
					MessageBox.Show("Если поле не принимает null, то должно быть задано значение по умолчанию.", "Значение по умолчанию");
				else
					MessageBox.Show("You must set the default value if the column does not allow null.", "Default value");
			}
			else
			{
				this.DialogResult = DialogResult.OK;
				this.Close();
			}
		}
		
		/// <summary>
		/// Validates the default value.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void ColDefaultBoxValidating(object sender, System.ComponentModel.CancelEventArgs e)
		{
			// Null is nothing
			if(this.colDefaultBox.Text == null || this.colDefaultBox.Text == "")
			{
				e.Cancel = false;
				return;
			}
			// Checking syntax
			if(this.colTypeCombo.SelectedIndex > 0 && this.colTypeCombo.SelectedIndex < 4)
			{
				// Digital format
				Regex re = new Regex(@"^\-?\d+$");
				if(re.IsMatch(this.colDefaultBox.Text))
				{
					e.Cancel = false;
				}
				else
				{
					if(culture == "ru")
						MessageBox.Show("Введите целое число согласно выбранному типу.", "Неверный ввод");
					else
						MessageBox.Show("Enter an integer according to the data type you have chosen.", "Wrong input");
					e.Cancel = true;
				}
			}
			else if(this.colTypeCombo.SelectedIndex == 4)
			{
				// Float format
				Regex re = new Regex(@"^\-?\d+\.\d+$");
				if(re.IsMatch(this.colDefaultBox.Text))
				{
					e.Cancel = false;
				}
				else
				{
					if(culture == "ru")
						MessageBox.Show("Введите десятичную дробь с плавающей точкой.", "Неверный ввод");
					else
						MessageBox.Show("Enter a valid float number.", "Wrong input");
					e.Cancel = true;
				}
			}
			else
			{
				e.Cancel = false;
			}
		}
	}
}
